//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Vml.Parsing
{
    internal struct QualifiedName
    {
        public override int GetHashCode()
        {
            throw new NotImplementedException();
        }
        public override string ToString()
        {
            if (string.IsNullOrEmpty(Prefix))
                return LocalName;
            return Prefix + ":" + LocalName;
            }
        public override bool Equals(object obj)
        {
            if (!(obj is QualifiedName))
                return false;
            var n = (QualifiedName) obj;
            return Equals(n);
        }

        public bool Equals(QualifiedName name)
        {
            return name.Prefix == Prefix && name.LocalName == LocalName;
        }
        public bool Equals (QualifiedName name, bool ignoreCase)
        {
            if (!ignoreCase)
                return Equals(name);
            return string.Compare(name.Prefix, Prefix, true) == 0 &&
                   string.Compare(name.LocalName, LocalName, true) == 0;
        }

        private readonly string _localName;

        private readonly string _prefix;
        public static QualifiedName Empty = new QualifiedName(string.Empty, string.Empty);

        public QualifiedName(string prefix, string localName)
        {
            Contract.Requires(localName != null);
            _prefix = prefix;
            _localName = localName;
        }
#if DEBUG
        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(_localName != null);
        }
#endif


        public string LocalName
        {
            get { return _localName; }
        }

        public string Prefix
        {
            get { return _prefix; }
        }

        public static QualifiedName Parse(string fullName)
        {
            if (fullName == null)
                return new QualifiedName(null, string.Empty);
            string[] ss = fullName.Split(':');
            switch (ss.Length)
            {
                case 1:
                    return new QualifiedName(null, ss[0]);
                case 2:
                    return new QualifiedName(ss[0], ss[1]);
                default:
                    throw new InvalidOperationException(fullName);
            }
        }
    }
}